'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

' Interaction logic for Contacts.xaml
Partial Public Class ContactsPage
    Inherits System.Windows.Controls.Page

    Private _ViewModel As ContactsPageViewModel

    Public ReadOnly Property ViewModel() As ContactsPageViewModel
        Get
            Return _ViewModel
        End Get
    End Property

    Public Sub New()
        _ViewModel = New ContactsPageViewModel

        InitializeComponent()
    End Sub

    Private Sub OnSendMessageClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim contact As ContactViewModel

        Try
            contact = DirectCast(DirectCast(e.OriginalSource, FrameworkElement).DataContext, ContactViewModel)

            ViewModel.StartChatSessionWithContact(contact)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnContactDoubleClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim contact As ContactViewModel

        Try
            contact = DirectCast(DirectCast(e.OriginalSource, FrameworkElement).DataContext, ContactViewModel)

            ViewModel.StartChatSessionWithContact(contact)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnSubMenuOpened(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim menu As ContextMenu
        Dim contact As ContactViewModel

        Try
            menu = DirectCast(e.OriginalSource, ContextMenu)
            contact = DirectCast(menu.DataContext, ContactViewModel)

            ViewModel.CreateContextMenuForContact(menu, contact)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnSetStatusClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim fe As FrameworkElement
        Dim status As IcqInterface.IcqStatusCode

        Try
            fe = DirectCast(e.OriginalSource, FrameworkElement)
            status = DirectCast(fe.DataContext, IcqInterface.IcqStatusCode)

            ViewModel.ChangeStatus(status)
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    'Private Sub OnStatusSelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    '    Dim status As JCQ.IcqInterface.IcqStatusCode

    '    Try
    '        status = DirectCast(e.AddedItems(0), IcqInterface.IcqStatusCode)

    '        ViewModel.ChangeStatus(status)
    '    Catch ex As Exception
    '        Core.Kernel.Exceptions.PublishException(ex)
    '    End Try
    'End Sub

    Private Sub OnAddNewContactClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim wnd As SearchWindow

        Try
            wnd = New SearchWindow
            wnd.Show()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Private Sub OnDisconnectClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            ViewModel.SignOut()
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub
End Class

Public Class StatusToImageConverter
    Implements System.Windows.Data.IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim status As IcqInterface.IcqStatusCode = TryCast(value, IcqInterface.IcqStatusCode)
        If status Is Nothing Then Return Nothing

        If status Is IcqInterface.IcqStatusCodes.Online Then
            Return App.Current.Resources("vbrOnline")
        ElseIf status Is IcqInterface.IcqStatusCodes.Offline Then
            Return App.Current.Resources("vbrOffline")
        Else
            Return App.Current.Resources("vbrAway")
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Debug.Fail("Unexpected call of ConvertBack")
        Return Nothing
    End Function
End Class